﻿using LivestreamE_Commerce.Dto.Tree;
using LivestreamE_Commerce.EntityFrameworkCore;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace LivestreamE_Commerce.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MyMenuController : ControllerBase
    {
        LivestreamE_CommerceDbContext db;
        public MyMenuController(LivestreamE_CommerceDbContext _db)
        {
            db = _db;
        }
        [HttpGet, Route("MenuTree")]
        public IActionResult MenuTree() 
        {
            var list = MenuTrees("");
            return Ok(new { list=list,state=true});
        }
        private List<MenuTreeDto> MenuTrees(string FenId) 
        {
            var list = db.MenuModel.ToList();
            var newlist = list.Where(x => Convert.ToString(x.FatherId).Equals(FenId)).ToList();
            List<MenuTreeDto> result = new List<MenuTreeDto>();
            newlist.ForEach(p =>
            {
                var t = new MenuTreeDto();
                t.Id = p.Id.ToString();
                t.Label = p.MenuName;
                t.Url = p.Url;
                t.Children = MenuTrees(p.Id.ToString());
                result.Add(t);
            });
            return result;
        }
    }
}
